<?php

/**
 * Menashop
 *
 * LICENSE
 *
 * This source file is subject to the license that is bundled
 * with this project in the file LICENSE.txt
 *
 * @category   Menashop
 * @package    Menashop_Content
 * @subpackage Features
 * @copyright  Copyright (c) 2011 Menashop UA Inc. (http://www.menashop.com.ua)
 * @license    LICENSE.txt
 * @version    1.0.0
 * @since      File available since Release 1.0.0
 */

/**
 * The SAX handler for features
 * 
 * @category   Menashop
 * @package    Menashop_Content
 * @subpackage Features
 * @copyright  Copyright (c) 2011 Menashop UA Inc. (http://www.menashop.com.ua)
 * @license    LICENSE.txt
 */
class Menashop_Content_Features
{

    /**
     * The data model for measure
     * @var Admin_Model_Measure
     */
    protected $_measureModel;

    /**
     * The data model for feature
     * @var Admin_Model_Feature
     */
    protected $_featureModel;

    /**
     * The data model for feature translation
     * @var Admin_Model_FeatureTranslation
     */
    protected $_featureTranslationModel;

    /**
     * IDs of languages
     * @var array 
     */
    protected $_languages;

    /**
     * The ID of the current language
     * @var int 
     */
    protected $_languageId;

    /**
     * Feature
     * @var Admin_Model_Feature 
     */
    protected $_feature;

    /**
     * Feature translations
     * @var array 
     */
    protected $_featureTranslations;

    /**
     * The flag for a tag 'Description'
     * @var integer
     */
    protected $_inDescription = 0;

    /**
     * The flag for a tag 'Name'
     * @var integer 
     */
    protected $_inName = 0;

    /**
     * The counter for features
     * @var integer 
     */
    protected $_totalFeatures = 0;

    /**
     * The counter for inserted features
     * @var integer 
     */
    protected $_totalFeaturesInserted = 0;

    /**
     * The counter for updated features
     * @var integer 
     */
    protected $_totalFeaturesUpdated = 0;

    /**
     * The counter for feature translations
     * @var integer 
     */
    protected $_totalFeatureTranslations = 0;

    /**
     * The counter for inserted feature translations
     * @var integer 
     */
    protected $_totalFeatureTranslationsInserted = 0;

    /**
     * The counter for updated feature translations
     * @var integer 
     */
    protected $_totalFeatureTranslationsUpdated = 0;

    /**
     * Initialize the SAX handler for features
     * @param array $languages 
     */
    public function __construct($languages)
    {
        $this->_measureModel = new Admin_Model_Measure();
        $this->_featureModel = new Admin_Model_Feature();
        $this->_featureTranslationModel = new Admin_Model_FeatureTranslation();
        $this->_languages = $languages;
    }

    /**
     * Process the start of an element in XML
     * @param obejct $parser
     * @param string $tag
     * @param array $attributes 
     */
    public function startElement($parser, $tag, $attributes)
    {
        if ('Feature' == $tag) {
            $this->_feature = array();
            $this->_feature['content_id'] = $attributes['ID'];
            $this->_feature['type'] = $attributes['Type'];
            $this->_feature['class'] = $attributes['Class'];
            $this->_featureTranslations = array();
        } elseif ('Name' == $tag) {
            // check a feature translation for the current language
            if (in_array($attributes['langid'], $this->_languages)) {
                // check a feature translation
                if (!isset($this->_featureTranslations[$attributes['langid']])) {
                    // create a new feature translation
                    $featureTranslation = array();
                    $featureTranslation['language_id'] = $attributes['langid'];
                    $this->_featureTranslations[$attributes['langid']] = &$featureTranslation;
                    $this->_totalFeatureTranslations++;
                }
                $this->_languageId = $attributes['langid'];
                $this->_inName = 1;
            }
        } elseif ('Description' == $tag) {
            // check a feature translation for the current language
            if (in_array($attributes['langid'], $this->_languages)) {
                // check a feature translation
                if (!isset($this->_featureTranslations[$attributes['langid']])) {
                    // create a new feature translation
                    $featureTranslation = array();
                    $featureTranslation['language_id'] = $attributes['langid'];
                    $this->_featureTranslations[$attributes['langid']] = &$featureTranslation;
                    $this->_totalFeatureTranslations++;
                }
                $this->_languageId = $attributes['langid'];
                $this->_inDescription = 1;
            }
        } elseif ('Measure' == $tag) {
            // find a measure in the database
            $measureId = $this->_measureModel->findIdByContentId($attributes['ID']);
            if ($measureId) {
                $this->_feature['measure_id'] = $measureId;
            }
        }
    }

    /**
     * Process the end of an element in XML
     * @param object $parser
     * @param string $tag 
     */
    public function endElement($parser, $tag)
    {
        if ('Feature' == $tag) {
            // find a feature in database
            $featureId = $this->_featureModel->findIdByContentId($this->_feature['content_id']);
            if ($featureId) {
                // update the feature
                $this->_feature['id'] = $featureId;
                $this->_totalFeaturesUpdated++;
            } else {
                // insert the feature
                $this->_totalFeaturesInserted++;
            }
            $featureId = $this->_featureModel->save($this->_feature);
            foreach ($this->_featureTranslations as $languageId => $featureTranslation) {
                $featureTranslationId = $this->_featureTranslationModel->findIdByFeatureAndLanguage($featureId, $languageId);
                if ($featureTranslationId) {
                    // update the feature translation
                    $featureTranslation['id'] = $featureTranslationId;
                    $this->_totalFeatureTranslationsUpdated++;
                } else {
                    // insert the feature translation
                    $this->_totalFeatureTranslationsInserted++;
                }
                $featureTranslation['feature_id'] = $featureId;
                $this->_featureTranslationModel->save($featureTranslation);
            }
            $this->_totalFeatures++;
        } elseif ('Name' == $tag) {
            $this->_inName = 0;
        } elseif ('Description' == $tag) {
            $this->_inDescription = 0;
        }
    }

    /**
     * Process characters in XML
     * @param object $parser
     * @param string $data 
     */
    public function characterData($parser, $data)
    {
        if ($this->_inName) {
            // check feature translation for the current language
            if (isset($this->_featureTranslations[$this->_languageId])) {
                $featureTranslation = &$this->_featureTranslations[$this->_languageId];
                if (isset($featureTranslation['name'])) {
                    $featureTranslation['name'] .= trim($data);
                } else {
                    $featureTranslation['name'] = trim($data);
                }
            }
        } elseif ($this->_inDescription) {
            // check feature translation for the current language
            if (isset($this->_featureTranslations[$this->_languageId])) {
                $featureTranslation = &$this->_featureTranslations[$this->_languageId];
                if (isset($featureTranslation['description'])) {
                    $featureTranslation['description'] .= trim($data);
                } else {
                    $featureTranslation['description'] = trim($data);
                }
            }
        }
    }

    /**
     * Get the report about a parsing
     * @return string 
     */
    public function getReport()
    {
        $report = "$this->_totalFeatures features in XML,";
        $report .= " $this->_totalFeaturesInserted inserted,";
        $report .= " $this->_totalFeaturesUpdated updated\n";
        $report .= "$this->_totalFeatureTranslations feature translations in XML,";
        $report .= " $this->_totalFeatureTranslationsInserted inserted,";
        $report .= " $this->_totalFeatureTranslationsUpdated updated";
        return $report;
    }

}


